package adapter

import (
	"bufio"
	"fmt"
	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc"
	"io"
	"os"
	"os/exec"
	"testing"
)

// AtomGit单元测试
func TestAtomGitDealDeployKeysSetting(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameDeployKeysSetting,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealDeployKeysSetting(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealWebHookSetting(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameWebHookSetting,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealWebHookSetting(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealPushRuleSetting(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNamePushRuleSetting,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealPushRuleSetting(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealCodeReviewSetting(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameCodeReviewSetting,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealCodeReviewSetting(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealProtectBranchSetting(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameProtectBranchSetting,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealProtectBranchSetting(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealRepoMetaInfo(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameRepoMetaInfo,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealRepoMetaInfo(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealRepoMember(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameRepoMetaInfo,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealRepoMember(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealRelease(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameRelease,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealRelease(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealMileStone(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameMileStone,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealMileStone(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealMergeRequest(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameMergeRequest,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealMergeRequest(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealIssue(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameIssue,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealIssue(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealCode(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId: 7696525,
		//RepoName:      "kernel",
		RepoGroupId:      5161228,
		RepoGroupName:    "openEuler",
		RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameCode,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealCode(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealWiki(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId: 7696525,
		//RepoName:      "kernel",
		RepoGroupId:      5161228,
		RepoGroupName:    "openEuler",
		RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameWiki,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = atomGitDealWiki(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealLabel(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           GlobalTaskNameLabel,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = globalAtomGitDealLabel(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealEnterprise(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           GlobalTaskNameEnterprise,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = globalAtomGitDealEnterprise(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealUser(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           GlobalTaskNameUser,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = globalAtomGitDealUser(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitDealGroup(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           GlobalTaskNameGroup,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
	}

	err = globalAtomGitDealGroup(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitCheckRepo(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId: 7696525,
		//RepoName:      "kernel",
		RepoGroupId:      5161228,
		RepoGroupName:    "openEuler",
		RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/aops-apollo.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameCode,
		RepoInfo:           &repoInfo,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
		Local:              true,
	}

	err = checkRepoCodeConsistent(args)
	assert.Equal(t, err, nil)
}

// AtomGit单元测试
func TestAtomGitCheckRepoMetaInfo(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId: 25286931,
		//RepoName:      "kernel",
		RepoGroupId:      5161228,
		RepoGroupName:    "openEuler",
		RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/aops-apollo.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	conn, err := CreateGrpcConnect(&cfg.BaseConfig)
	assert.Equal(t, err, nil)
	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(conn)

	args := TaskArgs{
		TaskName:           TaskNameRepoMetaInfo,
		RepoInfo:           &repoInfo,
		Token:              cfg.BaseConfig.Token,
		EnterpriseToken:    cfg.BaseConfig.EnterpriseToken,
		ExportPlatformName: GiteePlatformName,
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		Log:                log,
		Conn:               conn,
		AppKey:             cfg.BaseConfig.AppKey,
		AppSecret:          cfg.BaseConfig.AppSecret,
		Local:              true,
	}

	err = checkRepoMetaInfoConsistent(args)
	fmt.Println(err)
	assert.Equal(t, err, nil)
}

func TestStdoutStderr(t *testing.T) {
	fmt.Println(os.Getwd())

	//cmd := exec.Command("git", "clone", "--mirror", "https://gitee.com/openeuler/kernel.git")
	cmd := exec.Command("git", "clone", "--progress", "--mirror", "https://gitee.com/openeuler/gitbook-theme-hugo.git")

	// 获取标准错误管道
	stderrPipe, err := cmd.StderrPipe()
	if err != nil {
		panic(err)
	}

	// 启动命令
	if err := cmd.Start(); err != nil {
		panic(err)
	}

	// 读取标准错误
	go readOutput(stderrPipe, "STDERR")

	// 等待命令执行完成
	if err := cmd.Wait(); err != nil {
		fmt.Printf("Command finished with error: %v\n", err)
	}
}

// readOutput reads from io.Reader and prints the content to the console
func readOutput(r io.Reader, label string) {
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		fmt.Printf("[%s] %s\n", label, scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		fmt.Printf("Error reading from %s: %v\n", label, err)
	}
}
